package dog.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.sql.Timestamp;

import javax.swing.*;

import constants.CHANGE;
import containers.Room;
import dog.model.Model;

@SuppressWarnings("serial")
public class RoomList extends JPanel implements PropertyChangeListener
	{
	private JList list;
	private Model model;
	
	public RoomList(Model model)
		{
		this.model=model;
		model.addPropertyChangeListener(this);
		list = new JList(new DefaultListModel());
		list.setCellRenderer(new RoomCellRenderer());
		list.setPrototypeCellValue(new Room(-1,"Aaaaaaaaaaaaaaaa aa",-1));
		Room[] curr = model.gui_getCurrentRooms();
		for(int i=0; i<curr.length; i++)
			{
			((DefaultListModel)list.getModel()).addElement(curr[i]);
			}
		this.add(new JScrollPane(list));
		JButton refreshButton = new JButton("Refresh");
		refreshButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0)
				{
				//updateRoomAvailabilities();//TODO this function call should contain timing information.
				}});
		this.add(refreshButton);
		//updateRoomAvailabilities();
		}
	@Override
	public void propertyChange(PropertyChangeEvent e)
		{
		String type = e.getPropertyName();
		if (type.equals(CHANGE.ROOM_AVAILABILITY))
			{
			repaint();
			}
		else if (type.equals(CHANGE.ROOM))
			{	
			Room old = (Room)e.getOldValue();
			Room room = (Room)e.getNewValue();
			DefaultListModel model = (DefaultListModel)list.getModel();
			if (old!=null)
				model.removeElement(old);
			if (room!=null)
				model.addElement(room);
			repaint();
			}
		}
	}
